home *** CD-ROM | disk | FTP | other *** search
/ The Arsenal Files 3 / The Arsenal Files 3.iso / gen_prog / appdemo.exe / FILEIO.APP < prev    next >
Text File  |  1994-11-07  |  5KB  |  155 lines

  1. //                             FILEIO LIBRARY
  2. //                             ==============
  3.  
  4. // This file contains the library functions to Read or Write to files.
  5.  
  6. // You can modify it to suit your particular needs.  If you
  7. // do then it would be a good idea to lable all your changes with a unique
  8. // string in a comment so that if you get an upgrade you can remake those
  9. // changes.
  10.  
  11. // Using buffers significantly increases the speed of File I/O even if you
  12. // have a Cache program like SMARTDRV installed.  You can choose a value
  13. // for FILEBUFFERSIZE to suit your particular speed/memory needs.
  14.  
  15. #define FILEIO                // Makes some modifications to MAINLIB.APP file
  16.  
  17. #define FILEBUFFERSIZE  10000 // Number of bytes in the file buffer
  18. #define MAXHANDLES      20    // Maximum number of Open Files
  19.  
  20. int Open(AX_Name, byte ReadWriteType)
  21. {
  22.  // Opens a file for READ or WRITE
  23.  // Returns the file Handle or 0 if could not open file
  24.  int  Handle;
  25.  preserve DX;
  26.  
  27.  DX = AX_Name;
  28.  AX = 3D00h;               // DOS call to Open File to Read
  29.  if(ReadWriteType == WRITE) { AH = 3Ch; CX = 0; } // DOS Call To Open create file
  30.  PUSH DS; PUSH SS; POP DS; // Set up DS for DOS
  31.  INT 21h;                  // Attempt to open the File
  32.  POP DS;                   // Restore DS
  33.  if(CARRYFLAG) return 0;   // Could not open file
  34.  if(AX >= MAXHANDLES) End(4); // Too many open files so Terminate the Program
  35.  Handle = AX ;             // Keep the Handle for later
  36.  
  37.  AX = AllocateMemoryBlock(FILEBUFFERSIZE); // Ask for a block of memory
  38.  BX_Handle = Handle;
  39.  if(AX == 0)
  40.  {
  41.   // Allocation failed so Terminate program
  42.   AH = 3Eh; INT 21h;       // Call DOS to close file
  43.   End(Error);
  44.  }
  45.  Stream.BufferSegment[BX_Handle] = AX; // Segment address of File Buffer
  46.  Stream.BufferLength[BX_Handle] = 0;   // Position of last byte available to be read (ie. none)
  47.  Stream.BufferPosition[BX_Handle] = 0; // Position of next byte available
  48.  Stream.DOSPosition[BX_Handle] = 0;    // Position of DOS Pointer
  49.  Stream.Err[BX_Handle] = 0;            // No Error
  50.  Stream.ReadWriteType[BX_Handle] = ReadWriteType;
  51.  Stream.ColumnNumber[BX_Handle] = 0;
  52.  return BX_Handle;
  53. }
  54.  
  55.  
  56. void Close(BX_Handle)
  57. {
  58.  // Closes a file previously opened by Open()
  59.  
  60.  if(Stream.ReadWriteType[BX_Handle] == WRITE) Flush(BX_Handle);
  61.  AH = 3Eh; INT 21h;       // Call DOS to close file
  62.  if(CARRYFLAG) End(AX);   // Close failed so Terminate program
  63.  Stream.Err[BX_Handle] = 6; // Error for invalid handle
  64.  ReleaseMemoryBlock(Stream.BufferSegment[BX_Handle]);
  65. }
  66.  
  67.  
  68. int EOF(BX_Handle)
  69. {
  70.  // Checks if at the end of a file
  71.  // Returns 1 if at end of file or 0 if not at end of file
  72.  if(BX_Handle >= MAXHANDLES || Stream.Err[BX] == 6) End(4); // If invalid Handle then terminate program
  73.  AL = Stream.Err[BX_Handle]; AH = 0;
  74.  return AX;
  75. }
  76.  
  77.  
  78. long GetFilePosition(BX_Handle)
  79. {
  80.  // Get the Position of the next byte to be read for a File
  81.  // Note that the first byte is at position Zero.
  82.  
  83.  ECX = 0;
  84.  CX_BytesLeftInBuffer = Stream.BufferLength[BX_Handle] - Stream.BufferPosition[BX_Handle]; 
  85.  if(CX_BytesLeftInBuffer < 0) CX_BytesLeftInBuffer = 0; // Make sure that bytes left is positive
  86.  EAX = Stream.DOSPosition[BX] - ECX;
  87.  return EAX;
  88. }
  89.  
  90.  
  91. void SetFilePosition(BX_Handle, long Position)
  92. {
  93.  // Set the position from which to start reading or writing to next
  94.  preserve BX, EDX;
  95.  
  96.  // Note that the first byte is at position Zero.
  97.  
  98.  if(Stream.ReadWriteType[BX_Handle] == WRITE)
  99.  {
  100.   Flush(BX_Handle);
  101.   ECX = Position;  DX = CX;  // Least significant part
  102.   ECX = ECX >> 16;           // Most Significant part
  103.   //Dot();
  104.   AX = 0x4200; INT 21h;  // Move File Pointer  
  105.   if(CARRYFLAG) End(AX); // If Error Terminate Program
  106.  }
  107.  else
  108.  {
  109.   EDX = 0;
  110.   DX = Stream.BufferLength[BX_Handle]; 
  111.   EDX = Stream.DOSPosition[BX_Handle] - EDX;
  112.   if(Position < Stream.DOSPosition[BX_Handle] && Position >= EDX)
  113.   {
  114.    // Is within current Buffer
  115.    ECX = Position - EDX;
  116.    Stream.BufferPosition[BX_Handle] = CX;
  117.   }      
  118.   else
  119.   {
  120.    //PUSH BX; LogFile << "Outside Buffer\n"; POP BX;
  121.    Stream.BufferLength[BX_Handle] = 0;
  122.    Stream.BufferPosition[BX_Handle] = 0;
  123.    Stream.DOSPosition[BX_Handle] = Position;
  124.    ECX = Position;  DX = CX;  // Least significant part
  125.    ECX = ECX >> 16;           // Most Significant part
  126.    //Dot();
  127.    AX = 0x4200; INT 21h;  // Move File Pointer  
  128.    if(CARRYFLAG) End(AX); // If Error Terminate Program
  129.   }
  130.  }
  131. }
  132.  
  133.  
  134. void Flush(BX_Handle)
  135. {
  136.  // If there is stuff left in a stream buffer, then flush it out
  137.  preserve DX;
  138.  
  139.  if(BX < 5) return; // Devices below 5 are hardware devices
  140.  if(Stream.ReadWriteType[BX_Handle] != WRITE) End(5);
  141.  ECX = 0; CX = Stream.BufferPosition[BX_Handle];
  142.  Stream.BufferPosition[BX_Handle] = 0;
  143.  Stream.DOSPosition[BX_Handle] = Stream.DOSPosition[BX_Handle] + ECX;
  144.  Stream.BufferLength[BX_Handle] = 0;
  145.  DX = 0;
  146.  PUSH DS;
  147.  DS = Stream.BufferSegment[BX_Handle];
  148.  AH = 40h;
  149.  INT 21h;
  150.  POP DS;
  151.  if(CARRYFLAG) End(AX);
  152. }
  153.  
  154.  
  155.